﻿using NursingFormSystem.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;

namespace NursingFormSystem.PICCNursingForm
{
    public partial class PICCNursingRecordForm : Form
    {
        #region 变量声明
        public NursingFormSystem.NurFormsService.NursingForm formService = new NursingFormSystem.NurFormsService.NursingForm();
        Tb_User LoginUser;
        PatientInfo PatientInfo;
        #endregion

        #region 构造函数
        public PICCNursingRecordForm(Tb_User loginUser, PatientInfo patientInfo)
        {
            InitializeComponent();
            LoginUser = loginUser;
            PatientInfo = patientInfo;
            BindControl();
            BindInfo();
        }
        #endregion

        #region 新增
        private void btn_Add_Click(object sender, EventArgs e)
        {
            PICCNursingEntryForm pn = new PICCNursingEntryForm(LoginUser, PatientInfo, string.Empty);
            pn.ShowDialog();
            if (pn.DialogResult == DialogResult.OK)
            {
                BindInfo();
            }
        }
        #endregion

        #region 修改
        private void btn_Update_Click(object sender, EventArgs e)
        {
            if (bandedGridView1.RowCount > 0)
            {
                string Id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
                PICCNursingEntryForm gn = new PICCNursingEntryForm(LoginUser, PatientInfo, Id);
                gn.ShowDialog();
                if (gn.DialogResult == DialogResult.OK)
                {
                    BindInfo();
                }
            }
        }
        #endregion

        #region 查询公用方法
        /// <summary>
        /// 设置控件属性
        /// </summary>
        public void BindControl()
        {
            dateE_StartTime.DateTime = DateTime.Now.AddDays(-30);
            dateE_EndTime.DateTime = DateTime.Now;
        }

        /// <summary>
        /// 依据日期区间查询
        /// </summary>
        /// <param name="dateE_StartTime"></param>
        /// <param name="dateE_EndTime"></param>
        public void BindInfo()
        {
            if (this.PatientInfo == null) return;
            string patientid = this.PatientInfo.PATIENT_ID;   //住院号
            int VisitId = this.PatientInfo.VISIT_ID;   //住院次数
            int FormId = (int)FormType.PICCNursing;    //表单ID
            DataTable dt = new DataTable();
            dt.Columns.Add("RiQi", typeof(string));
            dt.Columns.Add("ShiJian", typeof(string));//时间
            dt.Columns.Add("AreaId", typeof(string));//病区
            dt.Columns.Add("WaiLuChangDu", typeof(string));//外露长度
            dt.Columns.Add("ShangBiWei", typeof(string)); //上臂围或腿围
            dt.Columns.Add("GuanDaoTongChang", typeof(string));//管道是否通畅
            dt.Columns.Add("GengHuanGanSuMao", typeof(string));//更换肝素帽
            dt.Columns.Add("GengHuanFuTie", typeof(string));//更换敷贴
            dt.Columns.Add("ChongGuanYeTi", typeof(string));//冲管液体
            dt.Columns.Add("FengGuan", typeof(string));//封管
            dt.Columns.Add("ChuanCiQingKuang", typeof(string));
            dt.Columns.Add("ChuanCiQingKuang1", typeof(string));
            dt.Columns.Add("ChuanCiQingKuang2", typeof(string));
            dt.Columns.Add("ChuanCiQingKuang3", typeof(string));
            dt.Columns.Add("ChuanCiQingKuang4", typeof(string));
            dt.Columns.Add("HuLiCuoShi", typeof(string));//护理措施
            dt.Columns.Add("HuShiQiamMing", typeof(string));//签名
            dt.Columns.Add("Id", typeof(string));
            var ret = formService.GetNursingFormRecordByDate(patientid, VisitId, FormId, dateE_StartTime.DateTime, dateE_EndTime.DateTime);
            if (ret.Code == 1)
            {
                List<NursingFormRecord> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<NursingFormRecord>>(ret.D);
                list = list.OrderByDescending(p=>p.AssessmentDate).ToList();
                foreach (NursingFormRecord record in list)
                {
                    var NursingFormData = record.NursingFormData;
                    PICCNursingRecord py = new PICCNursingRecord();
                    py = XmlUtil.XMLToObject<PICCNursingRecord>(NursingFormData, Encoding.UTF8);
                    DataRow row = dt.NewRow();
                    row["RiQi"] = Convert.ToDateTime(py.RiQi).ToString("yyyy-MM-dd");   //日期
                    row["ShiJian"] = DateTimeHelper.HHmmssToHHmm(py.ShiJian);   //时间
                    row["AreaId"] = PatientInfo.NURSE_CELL_NAME;  //病区
                    row["WaiLuChangDu"] = py.WaiLuChangDu;   //外露长度
                    row["ShangBiWei"] = py.ShangBiWei;    //上臂围或腿围
                    if (!string.IsNullOrEmpty(py.GuanDaoTongChang))
                        row["GuanDaoTongChang"] = "√";   //管道是否通畅
                    if (!string.IsNullOrEmpty(py.GengHuanGanSuMao))
                        row["GengHuanGanSuMao"] = "√";  //更换肝素帽
                    if (!string.IsNullOrEmpty(py.GengHuanFuTie))
                        row["GengHuanFuTie"] = "√";  //更换敷贴
                    row["ChongGuanYeTi"] = py.ChongGuanYeTi;    //冲管液体
                    row["FengGuan"] = py.FengGuan;    //封管
                    //穿刺部位情况
                    string ChuanCiQingKuang = py.ChuanCiQingKuang;
                    string[] ChuanArray;
                    if (!string.IsNullOrEmpty(ChuanCiQingKuang))
                    {
                        ChuanArray = ChuanCiQingKuang.Split('#');
                        if (Convert.ToInt32(ChuanArray[0]) == 1)
                            row["ChuanCiQingKuang"] = "√";
                        if (Convert.ToInt32(ChuanArray[1]) == 1)
                            row["ChuanCiQingKuang1"] = "√";
                        if (Convert.ToInt32(ChuanArray[2]) == 1)
                            row["ChuanCiQingKuang2"] = "√";
                        if (Convert.ToInt32(ChuanArray[3]) == 1)
                            row["ChuanCiQingKuang3"] = "√";
                        if (Convert.ToInt32(ChuanArray[4]) == 1)
                            row["ChuanCiQingKuang4"] = "√";
                    }
                    row["HuLiCuoShi"] = py.HuLiCuoShi;   //护理措施
                    row["HuShiQiamMing"] = py.QianMing;    //签名                    
                    row["Id"] = record.Id;//ID隐藏，双击触发事件
                    dt.Rows.Add(row);
                }
            }
            dt.DefaultView.Sort = "RiQi DESC,ShiJian DESC";
            DataTable data = dt.DefaultView.ToTable();
            gridControl1.DataSource = data;
        }
        #endregion

        #region 查询
        private void btn_Query_Click(object sender, EventArgs e)
        {
            BindInfo();
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Del_Click(object sender, EventArgs e)
        {
            if (bandedGridView1.RowCount > 0)
            {
                if (MessageBox.Show("确定要删除数据吗？", "删除前确认", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    string id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
                    var ret2 = formService.DeleteNursingFormRecord(id);
                    MessageBox.Show(ret2.M);
                    if (ret2.Code == 1) BindInfo();
                }
            }
        }
        #endregion

        #region 打印
        /// <summary>
        /// 打印
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_PrintView_Click(object sender, EventArgs e)
        {
            PICCNursingPrint f = new PICCNursingPrint(PatientInfo, dateE_StartTime.DateTime, dateE_EndTime.DateTime);
            f.Show();
        }
        #endregion

        #region 双击事件
        private void bandedGridView1_DoubleClick(object sender, EventArgs e)
        {
            if (bandedGridView1.DataRowCount <= 0) return;
            string Id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
            PICCNursingEntryForm add = new PICCNursingEntryForm(LoginUser, PatientInfo, Id);
            add.ShowDialog();
            if (add.DialogResult == DialogResult.OK)
            {
                BindInfo();
            }
        }
        #endregion
    }
}